package org.bgee.psp4jdbc.jdbcapi;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.Ref;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Map;

import org.bgee.psp4jdbc.pool.Pool;

/**
 * The aim of this class is to wrap a real <code>java.sql.CallableStatement</code> that
 * will be kept in a <code>PreparedStatement</code> pool when its <code>close</code>
 * method is called, instead of being really close. Every time the 
 * <code>CallableStatement</code> goes into the pool, its parameters are cleared.
 * <p>
 * It will stay in the pool until its parent {@link Connection} brings it back to
 * reuse it.
 * 
 * @author Mathieu Seppey
 * @author Frederic Bastian
 * @version 1, August 2013
 * @since 1
 *
 */
public class CallableStatement extends PreparedStatement implements java.sql.CallableStatement
{

    /**
     * <code>Logger</code> of the class. 
     */
    // Nothing to log yet. Comment to avoid a warning
//     private final static Logger log = 
//             LogManager.getLogger(CallableStatement.class.getName());

    /**
     * Default constructor, should not be used. 
     * Constructor protected, so that only a {@link Connection} can provide 
     * a <code>CallableStatement</code>.
     * @throws SQLException 
     */
    // Constructor not public, so that only a Connection can provide a CallableStatement.
    // suppress warnings
    @SuppressWarnings("unused")
    private CallableStatement() throws SQLException 
    {
        this(null, null, null,null);
    }

    /**
     * Constructor used to provide the real <code>java.sql.CallableStatement</code> 
     * that this class wraps, the <code>Connection</code> used to obtain 
     * this <code>CallableStatement</code>, and the <code>Pool</code> related to it.
     *  
     * @param id                        A <code>String</code> that represent the unique id
     *                                  of the <code>CallableStatement</code>.
     * @param pool 						The <code>Pool</code> holding this 
     * 									<code>CallableStatement</code>.
     * @param connection                The <code>java.sql.Connection</code> that was used 
     *                                  to obtain this <code>CallableStatement</code>.
     * @param realCallableStatement     The <code>java.sql.CallableStatement</code> 
     *                                  that this class wraps
     *                                                         
     */
    public CallableStatement(String id, Pool pool, java.sql.Connection connection,
            java.sql.CallableStatement realCallableStatement)
    {
        // call of the parent PreparedStatement constructor
        super(id, pool, connection, realCallableStatement);
    }
    
    public java.sql.CallableStatement getRealCallableStatement() {
    	return (java.sql.CallableStatement) this.getRealStatement();
    }
    
    //*******************************************************************************
    // Methods that are simply delegated and not already covered by PreparedStatement
    //*******************************************************************************
    
    @Override
    public void registerOutParameter(int parameterIndex, int sqlType)
            throws SQLException
    {
        this.getRealCallableStatement().registerOutParameter(parameterIndex, sqlType);
    }

    @Override
    public void registerOutParameter(int parameterIndex, int sqlType, int scale)
            throws SQLException
    {
        this.getRealCallableStatement().registerOutParameter(parameterIndex, sqlType,
                scale);
    }

    @Override
    public boolean wasNull() throws SQLException
    {
        return this.getRealCallableStatement().wasNull();
    }

    @Override
    public String getString(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getString(parameterIndex);
    }

    @Override
    public boolean getBoolean(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getBoolean(parameterIndex);
    }

    @Override
    public byte getByte(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getByte(parameterIndex);
    }

    @Override
    public short getShort(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getShort(parameterIndex);
    }

    @Override
    public int getInt(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getInt(parameterIndex);
    }

    @Override
    public long getLong(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getLong(parameterIndex);
    }

    @Override
    public float getFloat(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getFloat(parameterIndex);
    }

    @Override
    public double getDouble(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getDouble(parameterIndex);
    }

    @Override
    @Deprecated
    public BigDecimal getBigDecimal(int parameterIndex, int scale)
            throws SQLException
    {
        return this.getRealCallableStatement().getBigDecimal(parameterIndex, scale);
    }

    @Override
    public byte[] getBytes(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getBytes(parameterIndex);
    }

    @Override
    public Date getDate(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getDate(parameterIndex);
    }

    @Override
    public Time getTime(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getTime(parameterIndex);
    }

    @Override
    public Timestamp getTimestamp(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getTimestamp(parameterIndex);
    }

    @Override
    public Object getObject(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getObject(parameterIndex);
    }


    @Override
    public BigDecimal getBigDecimal(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getBigDecimal(parameterIndex);
    }

    @Override
    public Object getObject(int parameterIndex, Map<String, Class<?>> map)
            throws SQLException
    {
        return this.getRealCallableStatement().getObject(parameterIndex, map);
    }

    @Override
    public Ref getRef(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getRef(parameterIndex);
    }

    @Override
    public Blob getBlob(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getBlob(parameterIndex);
    }

    @Override
    public Clob getClob(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getClob(parameterIndex);
    }

    @Override
    public Array getArray(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getArray(parameterIndex);
    }

    @Override
    public Date getDate(int parameterIndex, Calendar cal) throws SQLException
    {
        return this.getRealCallableStatement().getDate(parameterIndex, cal);
    }

    @Override
    public Time getTime(int parameterIndex, Calendar cal) throws SQLException
    {
        return this.getRealCallableStatement().getTime(parameterIndex, cal);
    }

    @Override
    public Timestamp getTimestamp(int parameterIndex, Calendar cal)
            throws SQLException
    {
        return this.getRealCallableStatement().getTimestamp(parameterIndex, cal);
    }

    @Override
    public void registerOutParameter(int parameterIndex, int sqlType,
            String typeName) throws SQLException
    {
        this.getRealCallableStatement().registerOutParameter(parameterIndex, sqlType,
                typeName);
    }

    @Override
    public void registerOutParameter(String parameterName, int sqlType)
            throws SQLException
    {
        this.getRealCallableStatement().registerOutParameter(parameterName, sqlType);
    }

    @Override
    public void registerOutParameter(String parameterName, int sqlType,
            int scale) throws SQLException
    {
        this.getRealCallableStatement().registerOutParameter(parameterName, sqlType,
                scale);
    }

    @Override
    public void registerOutParameter(String parameterName, int sqlType,
            String typeName) throws SQLException
    {
        this.getRealCallableStatement().registerOutParameter(parameterName, sqlType,
                typeName);
    }

    @Override
    public URL getURL(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getURL(parameterIndex);
    }

    @Override
    public void setURL(String parameterName, URL val) throws SQLException
    {
        this.getRealCallableStatement().setURL(parameterName, val);
    }

    @Override
    public void setNull(String parameterName, int sqlType) throws SQLException
    {
        this.getRealCallableStatement().setNull(parameterName, sqlType);
    }

    @Override
    public void setBoolean(String parameterName, boolean x) throws SQLException
    {
        this.getRealCallableStatement().setBoolean(parameterName, x);
    }

    @Override
    public void setByte(String parameterName, byte x) throws SQLException
    {
        this.getRealCallableStatement().setByte(parameterName, x);
    }

    @Override
    public void setShort(String parameterName, short x) throws SQLException
    {
        this.getRealCallableStatement().setShort(parameterName, x);
    }

    @Override
    public void setInt(String parameterName, int x) throws SQLException
    {
        this.getRealCallableStatement().setInt(parameterName, x);
    }

    @Override
    public void setLong(String parameterName, long x) throws SQLException
    {
        this.getRealCallableStatement().setLong(parameterName, x);
    }

    @Override
    public void setFloat(String parameterName, float x) throws SQLException
    {
        this.getRealCallableStatement().setFloat(parameterName, x);
    }

    @Override
    public void setDouble(String parameterName, double x) throws SQLException
    {
        this.getRealCallableStatement().setDouble(parameterName, x);
    }

    @Override
    public void setBigDecimal(String parameterName, BigDecimal x)
            throws SQLException
    {
        this.getRealCallableStatement().setBigDecimal(parameterName, x);
    }

    @Override
    public void setString(String parameterName, String x) throws SQLException
    {
        this.getRealCallableStatement().setString(parameterName, x);
    }

    @Override
    public void setBytes(String parameterName, byte[] x) throws SQLException
    {
        this.getRealCallableStatement().setBytes(parameterName, x);
    }

    @Override
    public void setDate(String parameterName, Date x) throws SQLException
    {
        this.getRealCallableStatement().setDate(parameterName, x);
    }

    @Override
    public void setTime(String parameterName, Time x) throws SQLException
    {
        this.getRealCallableStatement().setTime(parameterName, x);
    }

    @Override
    public void setTimestamp(String parameterName, Timestamp x)
            throws SQLException
    {
        this.getRealCallableStatement().setTimestamp(parameterName, x);
    }

    @Override
    public void setAsciiStream(String parameterName, InputStream x, int length)
            throws SQLException
    {
        this.getRealCallableStatement().setAsciiStream(parameterName, x, length);
    }

    @Override
    public void setBinaryStream(String parameterName, InputStream x, int length)
            throws SQLException
    {
        this.getRealCallableStatement().setBinaryStream(parameterName, x, length);
    }

    @Override
    public void setObject(String parameterName, Object x, int targetSqlType,
            int scale) throws SQLException
    {
        this.getRealCallableStatement().setObject(parameterName, x, targetSqlType, scale);
    }

    @Override
    public void setObject(String parameterName, Object x, int targetSqlType)
            throws SQLException
    {
        this.getRealCallableStatement().setObject(parameterName, x, targetSqlType);
    }

    @Override
    public void setObject(String parameterName, Object x) throws SQLException
    {
        this.getRealCallableStatement().setObject(parameterName, x);
    }

    @Override
    public void setCharacterStream(String parameterName, Reader reader,
            int length) throws SQLException
    {
        this.getRealCallableStatement().setCharacterStream(parameterName, reader, length);
    }

    @Override
    public void setDate(String parameterName, Date x, Calendar cal)
            throws SQLException
    {
        this.getRealCallableStatement().setDate(parameterName, x, cal);
    }

    @Override
    public void setTime(String parameterName, Time x, Calendar cal)
            throws SQLException
    {
        this.getRealCallableStatement().setTime(parameterName, x, cal);
    }

    @Override
    public void setTimestamp(String parameterName, Timestamp x, Calendar cal)
            throws SQLException
    {
        this.getRealCallableStatement().setTimestamp(parameterName, x, cal);
    }

    @Override
    public void setNull(String parameterName, int sqlType, String typeName)
            throws SQLException
    {
        this.getRealCallableStatement().setNull(parameterName, sqlType, typeName);
    }

    @Override
    public String getString(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getString(parameterName);
    }

    @Override
    public boolean getBoolean(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getBoolean(parameterName);
    }

    @Override
    public byte getByte(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getByte(parameterName);
    }

    @Override
    public short getShort(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getShort(parameterName);
    }

    @Override
    public int getInt(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getInt(parameterName);
    }

    @Override
    public long getLong(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getLong(parameterName);
    }

    @Override
    public float getFloat(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getFloat(parameterName);
    }

    @Override
    public double getDouble(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getDouble(parameterName);
    }

    @Override
    public byte[] getBytes(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getBytes(parameterName);
    }

    @Override
    public Date getDate(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getDate(parameterName);
    }

    @Override
    public Time getTime(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getTime(parameterName);
    }

    @Override
    public Timestamp getTimestamp(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getTimestamp(parameterName);
    }

    @Override
    public Object getObject(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getObject(parameterName);
    }

    @Override
    public BigDecimal getBigDecimal(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getBigDecimal(parameterName);
    }

    @Override
    public Object getObject(String parameterName, Map<String, Class<?>> map)
            throws SQLException
    {
        return this.getRealCallableStatement().getObject(parameterName, map);
    }

    @Override
    public Ref getRef(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getRef(parameterName);
    }

    @Override
    public Blob getBlob(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getBlob(parameterName);
    }

    @Override
    public Clob getClob(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getClob(parameterName);
    }

    @Override
    public Array getArray(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getArray(parameterName);
    }

    @Override
    public Date getDate(String parameterName, Calendar cal) throws SQLException
    {
        return this.getRealCallableStatement().getDate(parameterName, cal);
    }

    @Override
    public Time getTime(String parameterName, Calendar cal) throws SQLException
    {
        return this.getRealCallableStatement().getTime(parameterName, cal);
    }

    @Override
    public Timestamp getTimestamp(String parameterName, Calendar cal)
            throws SQLException
    {
        return this.getRealCallableStatement().getTimestamp(parameterName, cal);
    }

    @Override
    public URL getURL(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getURL(parameterName);
    }

    @Override
    public RowId getRowId(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getRowId(parameterIndex);
    }

    @Override
    public RowId getRowId(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getRowId(parameterName);
    }

    @Override
    public void setRowId(String parameterName, RowId x) throws SQLException
    {
        this.getRealCallableStatement().setRowId(parameterName, x);
    }

    @Override
    public void setNString(String parameterName, String value)
            throws SQLException
    {
        this.getRealCallableStatement().setNString(parameterName, value);
    }

    @Override
    public void setNCharacterStream(String parameterName, Reader value,
            long length) throws SQLException
    {
        this.getRealCallableStatement().setNCharacterStream(parameterName, value, length);
    }

    @Override
    public void setNClob(String parameterName, NClob value) throws SQLException
    {
        this.getRealCallableStatement().setNClob(parameterName, value);
    }

    @Override
    public void setClob(String parameterName, Reader reader, long length)
            throws SQLException
    {
        this.getRealCallableStatement().setClob(parameterName, reader, length);
    }

    @Override
    public void setBlob(String parameterName, InputStream inputStream,
            long length) throws SQLException
    {
        this.getRealCallableStatement().setBlob(parameterName, inputStream, length);
    }

    @Override
    public void setNClob(String parameterName, Reader reader, long length)
            throws SQLException
    {
        this.getRealCallableStatement().setNClob(parameterName, reader, length);
    }

    @Override
    public NClob getNClob(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getNClob(parameterIndex);
    }

    @Override
    public NClob getNClob(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getNClob(parameterName);
    }

    @Override
    public void setSQLXML(String parameterName, SQLXML xmlObject)
            throws SQLException
    {
        this.getRealCallableStatement().setSQLXML(parameterName, xmlObject);
    }

    @Override
    public SQLXML getSQLXML(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getSQLXML(parameterIndex);
    }

    @Override
    public SQLXML getSQLXML(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getSQLXML(parameterName);
    }

    @Override
    public String getNString(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getNString(parameterIndex);
    }

    @Override
    public String getNString(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getNString(parameterName);
    }

    @Override
    public Reader getNCharacterStream(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getNCharacterStream(parameterIndex);
    }

    @Override
    public Reader getNCharacterStream(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getNCharacterStream(parameterName);
    }

    @Override
    public Reader getCharacterStream(int parameterIndex) throws SQLException
    {
        return this.getRealCallableStatement().getCharacterStream(parameterIndex);
    }

    @Override
    public Reader getCharacterStream(String parameterName) throws SQLException
    {
        return this.getRealCallableStatement().getCharacterStream(parameterName);
    }

    @Override
    public void setBlob(String parameterName, Blob x) throws SQLException
    {
        this.getRealCallableStatement().setBlob(parameterName, x);
    }

    @Override
    public void setClob(String parameterName, Clob x) throws SQLException
    {
        this.getRealCallableStatement().setClob(parameterName, x);
    }

    @Override
    public void setAsciiStream(String parameterName, InputStream x, long length)
            throws SQLException
    {
        this.getRealCallableStatement().setAsciiStream(parameterName, x, length);
    }

    @Override
    public void setBinaryStream(String parameterName, InputStream x, long length)
            throws SQLException
    {
        this.getRealCallableStatement().setBinaryStream(parameterName, x, length);
    }

    @Override
    public void setCharacterStream(String parameterName, Reader reader,
            long length) throws SQLException
    {
        this.getRealCallableStatement().setCharacterStream(parameterName, reader, length);
    }

    @Override
    public void setAsciiStream(String parameterName, InputStream x)
            throws SQLException
    {
        this.getRealCallableStatement().setAsciiStream(parameterName, x);
    }

    @Override
    public void setBinaryStream(String parameterName, InputStream x)
            throws SQLException
    {
        this.getRealCallableStatement().setBinaryStream(parameterName, x);
    }

    @Override
    public void setCharacterStream(String parameterName, Reader reader)
            throws SQLException
    {
        this.getRealCallableStatement().setCharacterStream(parameterName, reader);
    }

    @Override
    public void setNCharacterStream(String parameterName, Reader value)
            throws SQLException
    {
        this.getRealCallableStatement().setNCharacterStream(parameterName, value);
    }

    @Override
    public void setClob(String parameterName, Reader reader)
            throws SQLException
    {
        this.getRealCallableStatement().setClob(parameterName, reader);
    }

    @Override
    public void setBlob(String parameterName, InputStream inputStream)
            throws SQLException
    {
        this.getRealCallableStatement().setBlob(parameterName, inputStream);
    }

    @Override
    public void setNClob(String parameterName, Reader reader)
            throws SQLException
    {
        this.getRealCallableStatement().setNClob(parameterName, reader);
    }

    @Override
    public <T> T getObject(int parameterIndex, Class<T> type)
            throws SQLException
    {
        return this.getRealCallableStatement().getObject(parameterIndex, type);
    }

    @Override
    public <T> T getObject(String parameterName, Class<T> type)
            throws SQLException
    {
        return this.getRealCallableStatement().getObject(parameterName, type);
    }       


}
